package com.example.batchremovedemo;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.batchremovedemo.MyAdapter.OnShowItemClickListener;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends Activity implements OnShowItemClickListener {

	private ListView listView;
	private List<ItemBean> dataList;
	private List<ItemBean> selectList;
	private MyAdapter adapter;
	private RelativeLayout rootView;
	private LinearLayout opreateView;
	private static boolean isShow; // 是否显示CheckBox标识

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		listView = (ListView) findViewById(R.id.main_listview);
		dataList = new ArrayList<ItemBean>();
		selectList = new ArrayList<ItemBean>();

		for (int i=0; i<50; i++) {
			dataList.add(new ItemBean("第  " +i+ "项", false, false));
		}
		adapter = new MyAdapter(dataList, this);
		listView.setAdapter(adapter);
		adapter.setOnShowItemClickListener(this);

		listView.setOnItemLongClickListener(new OnItemLongClickListener() {

			@Override
			public boolean onItemLongClick(AdapterView<?> parent, View view,
					int position, long id) {
				if (isShow) {
					return false;
				} else {
					isShow = true;
					for (ItemBean bean : dataList) {
						bean.setShow(true);
					}
					adapter.notifyDataSetChanged();
					showOpervate();
					listView.setLongClickable(false);
				}
				return true;
			}
		});
		listView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				if (isShow) {
					ItemBean bean = dataList.get(position);
					boolean isChecked = bean.isChecked();
					if (isChecked) {
						bean.setChecked(false);
					} else {
						bean.setChecked(true);
					}
					adapter.notifyDataSetChanged();
				} else {
					Toast.makeText(MainActivity.this, dataList.get(position).getMsg(), Toast.LENGTH_SHORT).show();
				}
			}
		});
	}

	@Override
	public void onShowItemClick(ItemBean bean) {
		if (bean.isChecked() && !selectList.contains(bean)) {
			selectList.add(bean);
		} else if (!bean.isChecked() && selectList.contains(bean)) {
			selectList.remove(bean);
		}
	}

	/**
	 * 显示操作界面
	 */
	private void showOpervate() {
//		ViewGroup view = (ViewGroup) findViewById(R.id.main_view);
		LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		rootView = (RelativeLayout) findViewById(R.id.main_activity);
		opreateView = (LinearLayout) inflater.inflate(R.layout.opreate_view, null);
		RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,
				RelativeLayout.LayoutParams.WRAP_CONTENT);
		params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
		// 操作界面动画
		Animation anim = AnimationUtils.loadAnimation(this, R.anim.operate_in);
		opreateView.setAnimation(anim);
		rootView.addView(opreateView, params);
		// 返回、删除、全选和反选按钮初始化及点击监听
		TextView tvBack = (TextView) opreateView.findViewById(R.id.operate_back);
		TextView tvDelete = (TextView) opreateView.findViewById(R.id.operate_delete);
		final TextView tvSelect = (TextView) opreateView.findViewById(R.id.operate_select);
		tvSelect.setText("全选");
		tvBack.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (isShow) {
					selectList.clear();
					for (ItemBean bean : dataList) {
						bean.setChecked(false);
						bean.setShow(false);
					}
					adapter.notifyDataSetChanged();
					isShow = false;
					listView.setLongClickable(true);
					dismissOperate();
				}
			}
		});
		tvSelect.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if ("全选".equals(tvSelect.getText().toString())) {
					for (ItemBean bean : dataList) {
						if (!bean.isChecked()) {
							bean.setChecked(true);
							if (!selectList.contains(bean)) {
								selectList.add(bean);
							}
						}
					}
					adapter.notifyDataSetChanged();
					tvSelect.setText("反选");
				} else if ("反选".equals(tvSelect.getText().toString())) {
					for (ItemBean bean : dataList) {
						bean.setChecked(false);
						if (!selectList.contains(bean)) {
							selectList.remove(bean);
						}
					}
					adapter.notifyDataSetChanged();
					tvSelect.setText("全选");
				}
			}
		});
		tvDelete.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (selectList!=null && selectList.size()>0) {
					dataList.removeAll(selectList);
					adapter.notifyDataSetChanged();
					selectList.clear();
				} else {
					Toast.makeText(MainActivity.this, "请选择条目", Toast.LENGTH_SHORT).show();
				}
			}
		});
	}

	/**
	 * 隐藏操作界面
	 */
	private void dismissOperate() {
		if (opreateView != null) {
			Animation anim  = AnimationUtils.loadAnimation(MainActivity.this, R.anim.operate_out);
			opreateView.setAnimation(anim);
			rootView.removeView(opreateView);
		}
	}

	@Override
	protected void onResume() {
		super.onResume();
//		if (isShow && opreateView==null) {
//			isShow = false;
//		}
	}

	@Override
	public void onBackPressed() {
		if (isShow) {
			selectList.clear();
			for (ItemBean bean : dataList) {
				bean.setChecked(false);
				bean.setShow(false);
			}
			adapter.notifyDataSetChanged();
			isShow = false;
			listView.setLongClickable(true);
			dismissOperate();
		} else {
			super.onBackPressed();
		}
	}
}


